package mpYoga

import (
	"duoke/model"
	"duoke/service"
	"duoke/service/common"
	"duoke/utils/response"
	"fmt"
	"github.com/gin-gonic/gin"
	"time"
)

var User = new(userCtl)

type userCtl struct{}

func (ctl *userCtl) Info(c *gin.Context) {
	accounts, err := service.Request.User(c)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	where := fmt.Sprintf("id=%d", accounts.Id)
	var user model.ReqUser
	get, err := common.Tales.Info(where, "id,nickname,avatar,birthday,phone,sex").Get(&user)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	if !get {
		response.Response(c, 50008, err.Error(), nil)
		return
	}
	if user.Birthday != "" {
		// 定义解析模板（注意格式是固定的：Mon Jan 2 15:04:05 MST 2006）
		layout := "2006-01-02 15:04:05 -0700 MST"

		// 解析时间
		parsedTime, err := time.Parse(layout, user.Birthday)
		if err != nil {
			response.Response(c, 50008, "解析时间失败", nil)
			return
		}
		// 转换为 YYYY-MM-DD 格式
		user.Birthday = parsedTime.Format("2006-01-02")
	}
	response.Success(c, "获取用户信息成功", user)
}
